package info.guoyang.basic.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.ibatis.sqlmap.client.SqlMapClient;

//package info.guoyang.basic.util;
//
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.SQLException;
//import java.util.Properties;
//
//import javax.naming.Context;
//import javax.naming.InitialContext;
//import javax.naming.NamingException;
//import javax.sql.DataSource;
//
//import com.martian.utils.tools.LogTools;
//
//public class DBTools {
//
////	public static Connection getConnection() throws SQLException {
////		String url = "jdbc:db2://197.1.5.11:5912/TPPDBGYL";
////		String driver = "com.ibm.db2.jcc.DB2Driver";
////		Properties properties = new Properties();
////		properties.put("user", "ufgov");
////		properties.put("password", "ufgov1234");
////		try {
////			Class.forName(driver);
////			return DriverManager.getConnection(url, properties);
////		} catch (ClassNotFoundException e) {
////			LogTools.error(e);
////		}
////		return null;
////	}
//
//	public static Connection getJndiConnection() throws SQLException {
//		try {
//			Context context = new InitialContext();
//			DataSource ds = (DataSource) context.lookup("ufgov");
//			return ds.getConnection();
//		} catch (NamingException e) {
//			LogTools.error(e);
//		}
//		return null;
//	}
//}
public class DBTools {
	
	public static Connection getConnection(SqlMapClient sqlMap) {
		DataSource dataSource = sqlMap.getDataSource();
		
		Connection conn = null;
		
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static Statement getStatement(Connection conn) {
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stmt;
	}
	
	public static ResultSet getResultSet(Statement stmt,String sql) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public static ResultSet doAllDB(SqlMapClient sqlMap,String sql) {
		ResultSet rs = null;
		Connection conn = null;
		Statement stmt = null;
		
		try {
			conn = sqlMap.getDataSource().getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}
}